package com.yfdice.android.bam;

import android.content.Context;
import android.database.sqlite.*;
import android.database.SQLException;
import android.database.Cursor;
import android.content.ContentValues;

public class BluetoothDataBaseAdapter {
	//private static final String TAG2 = "BluetoothDataBaseAdapter";
	
	public static final String KEY_MAC = "macaddr";
	public static final String KEY_NAME = "name";
	public static final String KEY_USERNAME = "username";
	public static final String KEY_FILTER = "filter";
	
	private static final String DB_NAME = "monitor.db";
	private static final String DB_TABLE = "bluetooth";
	
	private static final int DB_VERSION = 1;
	
	private Context mContext = null;
	
	private static final String DB_CREATE = "CREATE TABLE IF NOT EXISTS " + DB_TABLE + " ("
											+ KEY_MAC + " TEXT PRIMARY KEY,"
											+ KEY_NAME + " TEXT,"
											+ KEY_USERNAME + " TEXT, "
											+ KEY_FILTER + " INTEGER)";
	private SQLiteDatabase mSQLiteDatabase = null;
	private DatabaseHelper mDatabaseHelper = null;
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, DB_NAME, null, DB_VERSION);
		}

		//@Override
		public void onCreate(SQLiteDatabase arg0) {
			// TODO Auto-generated method stub
			arg0.execSQL(DB_CREATE);
			
		}

		//@Override
		public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub
			arg0.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
			onCreate(arg0);
		}
	}
	
	public BluetoothDataBaseAdapter(Context context)
	{
		mContext = context;
	}
	
	public void open() throws SQLException
	{
		mDatabaseHelper = new DatabaseHelper(mContext);
		mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
	}
	
	public void close()
	{
		mDatabaseHelper.close();
	}
	
	public long insertData(String macAddr, String name, String username, int nFilter)
	{
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_MAC, macAddr);
		initialValues.put(KEY_NAME, name);
		initialValues.put(KEY_USERNAME, username);
		initialValues.put(KEY_FILTER, nFilter);
		return mSQLiteDatabase.insert(DB_TABLE, null, initialValues);
	}
	
	public boolean deleteData(String macAddr)
	{
		return mSQLiteDatabase.delete(DB_TABLE, KEY_MAC + "=?",  new String[] {macAddr} ) > 0;
	}
	
	public Cursor fetchAllData()
	{
		return mSQLiteDatabase.query(DB_TABLE, new String[] {KEY_MAC, KEY_NAME, KEY_USERNAME, KEY_FILTER}, 
				null, null, null, null, null);
	}
	
	public Cursor fetchData(String macAddr) throws SQLException
	{
		Cursor mCursor = mSQLiteDatabase.query(true, DB_TABLE,  new String[] {KEY_MAC, KEY_NAME, KEY_USERNAME, KEY_FILTER}, 
				KEY_MAC + "=" + macAddr, null, null, null, null, null);
	
		if (mCursor != null)
		{
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	public boolean updateData(String macAddr, String name, String username, int nFilter)
	{
		ContentValues args = new ContentValues();
		args.put(KEY_NAME, name);
		args.put(KEY_USERNAME, username);
		args.put(KEY_FILTER, nFilter);
		return mSQLiteDatabase.update(DB_TABLE, args, KEY_MAC + "=?", new String[] {macAddr}) > 0;
	}
};
